package com.thiswhyme.republish.rabbitmq.datasource;

import com.thiswhyme.republish.rabbitmq.core.domain.MessageInfo;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * MessageInfoSql组装器
 * Created by thiswhyme on 2021-03-21.
 */
public class MessageInfoSqlBuilder {

    public MessageInfoSqlBuilder(String tableName) {
        this.tableName = tableName;
    }

    private String tableName;

    private static final String BASE_COLUMNS = "id, type, channel_name, msg_id, headers, content, content_class_type, publish_status, retry_times, create_time, update_time";

    private static final String INSERT_SQL_TEMPLATE = "INSERT INTO %s (%s) VALUES (%s)";

    private static final String DELETE_SQL_TEMPLATE = "DELETE FROM %s WHERE id IN (%s)";

    private static final String SELECT_SQL_TEMPLATE = "SELECT %s FROM %s WHERE id IN (%s)";

    private static final String UPDATE_SQL_TEMPLATE = "UPDATE %s SET %s WHERE id = ?";

    private static final String CREATETIME_SELECT_SQL = "SELECT %s FROM %s WHERE publish_status IN (%s) AND create_time <= ? limit ?, ?";

    /**
     * 构建新增语句
     * @param suffix 历史表后缀（不传查实时表）
     * @return
     */
    public String buildInsertSql(String suffix) {
        suffix = suffix == null ? "" : suffix;
        Field[] fields = MessageInfo.class.getDeclaredFields();
        String values = Arrays.asList(fields).stream().map(field -> "?")
                .collect(Collectors.joining(", "));
        return String.format(INSERT_SQL_TEMPLATE, tableName + suffix, BASE_COLUMNS, values);
    }

    /**
     * 构建删除语句
     * @param dataIds
     * @param suffix 历史表后缀（不传查实时表）
     * @return
     */
    public String buildDeleteSql(List<Long> dataIds, String suffix) {
        String values = dataIds.stream().map(dataId -> "?")
                .collect(Collectors.joining(", "));
        return String.format(DELETE_SQL_TEMPLATE, tableName + suffix, values);
    }

    /**
     * 构建查询语句
     * @param dataIds
     * @return
     */
    public String buildSelectSql(List<Long> dataIds) {
        String values = dataIds.stream().map(dataId -> "?")
                .collect(Collectors.joining(", "));
        return String.format(SELECT_SQL_TEMPLATE, BASE_COLUMNS, tableName, values);
    }

    /**
     * 构建更新发送状态语句
     * @return
     */
    public String buildUpdateStatusSql() {
        String updateColumn = "publish_status = ?, update_time = ?";
        return String.format(UPDATE_SQL_TEMPLATE, tableName, updateColumn);
    }

    /**
     * 构建更新重试次数语句
     * @return
     */
    public String buildUpdateRetryTimesSql() {
        String updateColumn = "retry_times = ?, update_time = ?";
        return String.format(UPDATE_SQL_TEMPLATE, tableName, updateColumn);
    }

    /**
     * 构建创建时间查询语句
     * @param statusList
     * @return
     */
    public String buildSelectByCreateTimeSql(List<Integer> statusList) {
        String values = statusList.stream().map(status -> "?")
                .collect(Collectors.joining(", "));
        return String.format(CREATETIME_SELECT_SQL, BASE_COLUMNS, tableName, values);
    }
}
